{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f3f9f4c",
   "metadata": {},
   "source": [
    "列表生成式是Python内置的用来创建list的生成式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab074c28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,11))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "768df822",
   "metadata": {},
   "source": [
    "如果要生成[1x1,2x2,....10x10],常规思路是建立空表通过迭代完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4868b198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = []\n",
    "for x in range(1,11):\n",
    "    L.append(x * x)\n",
    "L"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3a5843f",
   "metadata": {},
   "source": [
    "通过循环表现的较为繁琐，列表生成式可以用一句话代替上面的循环："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0f2fd899",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x * x for x in range(1,11)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "451a92ca",
   "metadata": {},
   "source": [
    "写列表生成式，把要生成的元素x * x放在前面，后面跟for循环，就可以把list创建出来了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c80a876d",
   "metadata": {},
   "source": [
    "for循环后还可以加上if判断，比如筛选出偶数的平方："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9d30157a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 16, 36, 64, 100]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " [x * x for x in range(1,11) if x % 2 == 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adaa8862",
   "metadata": {},
   "source": [
    "还可以使用两层循环，生成全排列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba65aaa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[m + n for m in 'ABC' for n in 'XYZ']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37b493a4",
   "metadata": {},
   "source": [
    "运用列表生成式，可以写出简洁的代码。列出当前目录下所有文件和目录名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "466c1294",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints', '4.1 切片.ipynb', '4.2 迭代.ipynb', '4.3 列表生成式.ipynb']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "[d for d in os.listdir('.')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e472f2a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['x=A', 'y=B', 'z=C']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'x': 'A', 'y':'B', 'z':'C'}\n",
    "[k + '=' + v for k , v in d.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4430600",
   "metadata": {},
   "source": [
    "### if ... else"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b827ecd7",
   "metadata": {},
   "source": [
    "跟在for后面的if是一个筛选条件，不能带else。带了else则不是筛选而是条件判断了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b336c23",
   "metadata": {},
   "source": [
    "而把if放在for前面，则是一个表达式，因此必须根据x计算出一个结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3dfced64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x if x % 2 == 0 else -x for x in range(1,11)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90f845e0",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8105c23f",
   "metadata": {},
   "source": [
    "运用列表生成式，可以快速生成list，代码十分简洁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcd357ff",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
